package com.generalbackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.generalbackend.common.model.CommonResult;
import com.generalbackend.common.model.entity.QuartzJob;
import com.generalbackend.common.model.query.QuartzJobQuery;
import com.generalbackend.common.service.TimerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author tim.xie
 * @date 2021/12/8 17:17
 */
@RestController
@RequestMapping("/api/timer")
public class TimerController {

    @Autowired
    TimerService timerService;

    @PostMapping("/start")
    public CommonResult startJob(@RequestBody QuartzJob quartzJob) {
        return timerService.startTimer(quartzJob.getId());
    }

    @PostMapping("/add")
    public CommonResult addJob(@RequestBody QuartzJob quartzJob) {
        return timerService.addTimer(quartzJob);
    }

    @GetMapping("/pages/{limit}/{offset}")
    public CommonResult getTimerPages(@PathVariable Integer limit,
                                      @PathVariable Integer offset){
        return timerService.getTimerPages(limit,offset);
    }

    @PutMapping("/stop/{id}")
    public CommonResult stopTimer(@PathVariable Long id){
        return timerService.stopTimer(id);
    }

    @PostMapping("/delete/{id}")
    public CommonResult deleteTimer(@PathVariable Long id){
        QuartzJob quartzJob = new QuartzJob();
        quartzJob.setId(id);
        return timerService.deleteTimer(quartzJob);
    }

    @PostMapping("/pause/{id}")
    public CommonResult pauseTimer(@PathVariable Long id){
        QuartzJob quartzJob = new QuartzJob();
        quartzJob.setId(id);
        return timerService.pauseTimer(quartzJob);
    }

    @PostMapping("/resume/{id}")
    public CommonResult resumeTimer(@PathVariable Long id){
        QuartzJob quartzJob = new QuartzJob();
        quartzJob.setId(id);
        return timerService.resumeTimer(quartzJob);
    }

    @PostMapping("/page")
    public CommonResult getPage(@RequestBody QuartzJobQuery quartzJobQuery) {
        Page<QuartzJob> page = timerService.getPage(quartzJobQuery);
        return CommonResult.successCommonResult(page, "查询成功");
    }

    @PostMapping("/update")
    public CommonResult updateTimer(@RequestBody QuartzJob quartzJob) {
        timerService.updateQuartz(quartzJob);
        return CommonResult.successCommonResult("修改成功");
    }
}
